package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.comment.dtos.CommentDto;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageRequestDto;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.dtos.WmSensitiveDto;
import com.heima.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmSensitiveService;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 郭文杰
 * @description 针对表【wm_sensitive(敏感词信息表)】的数据库操作Service实现
 * @createDate 2024-04-06 23:57:27
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive>
        implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 敏感词分页查询
     *
     * @param pageRequestDto 页码 每页显示条数
     * @return 当前页显示列表
     */
    @Override
    public List<WmSensitive> pageSensitive(PageRequestDto pageRequestDto) {
        return null;
    }

    /**
     * 敏感词 分页查询
     *
     * @param pageRequestDto 页码 每页显示条数
     * @return 当前页显示内容
     */
    @Override
    public Page wmSensitivePage(PageRequestDto pageRequestDto) {
        //参数校验
        if (pageRequestDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        Page<WmSensitive> page = new Page<>(pageRequestDto.getPage(), pageRequestDto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(
                        pageRequestDto.getName() != null && StringUtils.isNotEmpty(pageRequestDto.getName()),
                        WmSensitive::getSensitives,
                        pageRequestDto.getName()
                )
                .orderByDesc(WmSensitive::getCreatedTime);
        wmSensitiveMapper.selectPage(page, wrapper);
        return page;
    }

    /**
     * 添加敏感词
     *
     * @param wmSensitiveDto 前端dto
     */
    @Override
    public void wmSensitiveSave(WmSensitiveDto wmSensitiveDto) {
        //非空校验
        if (wmSensitiveDto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查看敏感词是否存在
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(
                wmSensitiveDto.getSensitives() != null && StringUtils.isNotEmpty(wmSensitiveDto.getSensitives()),
                WmSensitive::getSensitives,
                wmSensitiveDto.getSensitives());
        WmSensitive wmSensitive = wmSensitiveMapper.selectOne(wrapper);
        if (wmSensitive != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        //敏感词不存在,添加敏感词
        wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(wmSensitiveDto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
    }

    /**
     * 扫描内容中的敏感词是否存在
     * @param commentDto 内容
     * @return true没有敏感词 false有敏感词
     */
    @Override
    public Boolean scan(CommentDto commentDto) {
        boolean rs = true;

        //select sensitives from wm_sensitive
        //查询条件 在敏感词库中查询不为null的记录
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(null);
        //过滤出所有 敏感词汇
        List<String> sensitivesList = wmSensitives
                .stream()
                .map(wmSensitive -> wmSensitive.getSensitives()).
                collect(Collectors.toList());
        //初始化敏感词库
        SensitiveWordUtil.initMap(sensitivesList);
        //查询文章内容文字中的敏感词
        String content = commentDto.getContent();
        Map<String, Integer> map = SensitiveWordUtil.matchWords(content);
        //如果有敏感词,返回false
        if (map.size() != 0) {
            rs = false;
        }
        return rs;
    }
}




